using JGSY.CMS.LowCode.Platform.Application.Interfaces;
using JGSY.CMS.LowCode.Platform.Application.DTOs;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace JGSY.CMS.LowCode.Platform.Controllers
{
    /// <summary>
    /// 双因素认证管理控制器
    /// 提供双因素认证相关API接口，包括密钥生成、令牌校验、备份码管理等
    /// 支持TOTP认证、备份码验证、认证状态管理等安全认证功能
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class TwoFactorController : ControllerBase
    {
        /// <summary>
        /// 双因素认证应用服务接口
        /// </summary>
        private readonly ITwoFactorAppService _service;
        /// <summary>
        /// 构造函数，注入双因素认证服务
        /// </summary>
        /// <param name="service">双因素认证应用服务接口</param>
        public TwoFactorController(ITwoFactorAppService service)
        {
            _service = service;
        }
    /// <summary>
        /// 生成双因素密钥
        /// </summary>
        /// <param name="username">用户名</param>
        /// <returns>密钥生成结果</returns>
    [HttpPost("setup")]
        public async Task<ActionResult<TwoFactorSetupResultDto>> Setup([FromBody] string username)
        {
            var result = await _service.GenerateTwoFactorSecretAsync(username);
            return Ok(result);
        }
    /// <summary>
        /// 校验双因素令牌
        /// </summary>
        /// <param name="req">令牌校验请求</param>
        /// <returns>校验结果</returns>
    [HttpPost("verify-token")]
        public async Task<ActionResult<bool>> VerifyToken([FromBody] TokenVerifyRequest req)
        {
            var result = await _service.VerifyTokenAsync(req.Token, req.Secret);
            return Ok(result);
        }
    /// <summary>
        /// 校验备份码
        /// </summary>
        /// <param name="req">备份码校验请求</param>
        /// <returns>校验结果</returns>
    [HttpPost("verify-backup")]
        public async Task<ActionResult<TwoFactorVerifyResultDto>> VerifyBackup([FromBody] BackupVerifyRequest req)
        {
            var result = await _service.VerifyBackupCodeAsync(req.Code, req.BackupCodes);
            return Ok(result);
        }
    /// <summary>
        /// 重新生成备份码
        /// </summary>
        /// <returns>备份码数组</returns>
    [HttpPost("regenerate-backup")]
        public async Task<ActionResult<string[]>> RegenerateBackup()
        {
            var result = await _service.RegenerateBackupCodesAsync();
            return Ok(result);
        }
    /// <summary>
        /// 获取双因素认证摘要信息
        /// </summary>
        /// <param name="data">双因素数据</param>
        /// <returns>摘要信息</returns>
    [HttpPost("summary")]
        public async Task<ActionResult<TwoFactorSummaryDto>> Summary([FromBody] TwoFactorDataDto data)
        {
            var result = await _service.GetTwoFactorSummaryAsync(data);
            return Ok(result);
        }
        /// <summary>
        /// 令牌校验请求 DTO
        /// </summary>
        public class TokenVerifyRequest
        {
            /// <summary>
            /// 令牌字符串
            /// </summary>
            public string Token { get; set; } = string.Empty;
            /// <summary>
            /// 密钥字符串
            /// </summary>
            public string Secret { get; set; } = string.Empty;
        }
        /// <summary>
        /// 备份码校验请求 DTO
        /// </summary>
        public class BackupVerifyRequest
        {
            /// <summary>
            /// 备份码
            /// </summary>
            public string Code { get; set; } = string.Empty;
            /// <summary>
            /// 备份码数组
            /// </summary>
            public string[] BackupCodes { get; set; } = new string[0];
        }
    }
}
